var currentStep:Number;
var currentParam:String;
var maxStep:Number=0;

var nextAllowed:Boolean;

var recorderMain:MovieClip;

function init(param, data, slot)
{
	recorderMain=mlf.gatekeeper.native.Caller.scanElement(slot, "main");
	Packages.system.Platform.renderPortlet(slot);
	currentStep=null;
	maxStep=0;
	// temp set the first step - view
	switchStep("_root-ReadingRecorder-main-rworkflow-view");
}

function switchStep(param, data, slot)
{
	var prefix="_root-ReadingRecorder-main-rworkflow-";
	var selectedStep:Number;
	if (param.indexOf("view")>-1) selectedStep=0;
	else if (param.indexOf("task")>-1) selectedStep=1;
	else if (param.indexOf("guide")>-1) selectedStep=2;
	else if (param.indexOf("space")>-1) selectedStep=3;
	else if (param.indexOf("polish")>-1) selectedStep=4;
	else if (param.indexOf("report")>-1) selectedStep=5;
	else selectedStep=param;

	// tmp
	if (param==0) param=prefix+"view";
	else if (param==1) param=prefix+"task";
	else if (param==2) param=prefix+"guide";
	else if (param==3) param=prefix+"space";
	else if (param==4) param=prefix+"polish";
	else if (param==5) param=prefix+"report";
	// end

	// switch permission 
	// related behavior
	// condition

	if (selectedStep<=maxStep || selecetedStep==0)
	{
		var bg=mlf.mcmanager.PathResolver.extractPath(param);
		bg._alpha=100;
		Packages.system.Platform.highlightBlock(bg);

		if (currentStep!=null && currentStep!=selecetedStep)
		{
			bg=mlf.mcmanager.PathResolver.extractPath(currentParam);
			bg._alpha=0;
		}
		// tmp
		// TODO: add to next step logic
		if (currentStep=="2" || currentStep=="3")
		{
			Packages.modula.TxtReader.removeInstance();
			// remove mic status listener
			var active_mic:Microphone=Microphone.get();
			active_mic.onActivity=null;
		}
		if (selecetedStep=="4")
		{
			applications.prototype.Logic.run("Stop");
		}
		Packages.components.Scene.switchRecordingFlow(currentStep, selectedStep);
		Packages.components.Scene.guide("dispose");
		Packages.system.Platform.messageHint("ReadingRecorder", "", "normal");
		currentStep=selectedStep;
		currentParam=param;
		// trace("selectedStep: "+selectedStep);
		// trace("maxStep: "+maxStep);
		if (selectedStep==maxStep) maxStep++;
	}
}

function pressStep(param, data, slot)
{
	if (currentStep=="5") return;
	if (param.indexOf("next")>-1 && !nextAllowed) return;
	if (param.indexOf("prev")>-1 && currentStep==0) return;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 100);	
}

function rolloverStep(param, data, slot)
{
	if (currentStep=="5") return;
	if (param.indexOf("next")>-1 && !nextAllowed) return;
	if (param.indexOf("prev")>-1 && currentStep==0) return;
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=false;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 50);	
}

function rolloutStep(param, data, slot)
{
	// if (param.indexOf("next")>-1 && !nextAllowed) return;
	// if (param.indexOf("prev")>-1 && currentStep==0) return;
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=true;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 0);	
}

function releaseStep(param, data, slot)
{
	if (currentStep=="5") return;
	if (param.indexOf("next")>-1 && !nextAllowed) return;
	if (param.indexOf("prev")>-1 && currentStep==0) return;
	var offset:Number;
	if (param.indexOf("prev")>-1 && currentStep>0)
	{
		switchStep(currentStep-1);
	}
	else if (param.indexOf("next")>-1 && currentStep<5)
	{
		switchStep(currentStep+1);
	}
	nextAllowed=false;
	rolloutStep(param, data, slot);
}

function refreshResultContent(cont:String, permission:Boolean)
{
	var rstContent=mlf.gatekeeper.native.Caller.scanElement("ReadingRecorder", "resultContent");
	// trace("refreshResultContent: "+rstContent+" "+cont);
	rstContent.htmlText=cont;
	nextAllowed=permission;
	// trace("nextAllowed: "+nextAllowed);
}

function refreshHelpContent(cont:String)
{
	var hlpContent=mlf.gatekeeper.native.Caller.scanElement("ReadingRecorder", "recordHelpContent");
	hlpContent.htmlText=cont;
}

function back2home()
{
	trace("back2home");
	// switch permission 
	// related behavior
	// condition
	applications.prototype.Logic.run("Stop");
	Packages.task.Composition.clear();
	Packages.task.Processor.clear();

	Packages.components.Scene.guide("dispose");
	Packages.components.Scene.recorder2home(currentStep);
}